package com.stylefeng.guns.modular.system.controller;


import com.stylefeng.guns.common.annotion.Permission;
import com.stylefeng.guns.common.annotion.log.BussinessLog;
import com.stylefeng.guns.common.constant.tips.Tip;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.enu.BillStateEnum;
import com.stylefeng.guns.common.enu.BillTypeEnum;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.model.Bill;
import com.stylefeng.guns.common.persistence.model.Dict;
import com.stylefeng.guns.common.persistence.model.Employee;
import com.stylefeng.guns.common.transform.BillTransform;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.DictDao;
import com.stylefeng.guns.modular.system.service.BillService;
import com.stylefeng.guns.modular.system.service.EmployeeService;
import com.stylefeng.guns.modular.system.warpper.UserWarpper;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;

/**
 * 前端控制器
 * @author yanqb
 * @since 2018-05-17
 */
@Controller
@RequestMapping("/bill")
public class BillController extends BaseController {

    private String PREFIX = "/system/bill/";

    @Resource
    private BillService service;

    @Resource
    DictDao dictDao;

    @Resource
    private EmployeeService empService;

    /**
     * 跳转首页
     */
    @RequestMapping(value={""})
    public String index() {
        return PREFIX + "bill_list.html";
    }

    /**
     * 查询账单
     */
    @ApiOperation("获取账单列表")
    @RequestMapping(value = "/bill_list", method = RequestMethod.POST)
    @ResponseBody
    public Object billList(@RequestParam(required = false) String content,
                           @RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime) {

//        List<Map<String, Object>> users = managerDao.selectUsers(name, beginTime, endTime);
//        return new UserWarpper(users).warp();

        List<Bill> list = this.service.search(content, startTime, endTime);
        for(Bill bill:list){
            bill.getId();
        }
        return BillTransform.transform(list);
    }

    /**
     * 跳转到申请报销页面
     */
    @RequestMapping("/to_apply_add")
    public String toApplyAdd(Model model) {
        ShiroUser user = ShiroKit.getUser();
        model.addAttribute("user", user);
        model.addAttribute("billTypeEnum", BillTypeEnum.values());
        model.addAttribute("billStateEnum", BillStateEnum.values());

        return PREFIX + "apply_add.html";
    }

    /**
     * 跳转到添加账单页面
     */
    @RequestMapping("/to_add")
    public String toAdd(Model model) {
        // 获取人员信息（页面直接选部门找员工）、获取账单类型、获取账单状态
//        List<Dict> dictList = dictDao.selectByCode("bill_type");

        List<Employee> employeeList = empService.search(null);
        model.addAttribute("employeeList", employeeList);
        model.addAttribute("billTypeEnum", BillTypeEnum.values());
        model.addAttribute("billStateEnum", BillStateEnum.values());
        return PREFIX + "bill_add.html";
    }

    /**
     * 录入账单
     */
    @ApiOperation("录入账单")
    @RequestMapping(value = "/bill_add", method = RequestMethod.POST)
    @BussinessLog(value = "录入账单", key = "account", dict = "UserDict")
    @ResponseBody
    public Tip addBill(@Valid Bill bill, BindingResult result) {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }

        // 完善信息
        bill.setCreateUser(Objects.requireNonNull(ShiroKit.getUser()).getId());
        bill.setCreateTime(new Date());
        service.addBill(bill);
        return SUCCESS_TIP;
    }

    /**
     * 申请流程 操作
     */
    @ApiOperation("申请流程")
    @RequestMapping(value = "/bill_execute", method = RequestMethod.POST)
    @BussinessLog(value = "申请流程", key = "account", dict = "UserDict")
    @ResponseBody
    public Tip executeBill(@Valid Bill bill, BindingResult result) {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }

        bill.setModifyUser(Objects.requireNonNull(ShiroKit.getUser()).getId());
        bill.setModifyTime(new Date());

        service.executeBill(bill);
        return SUCCESS_TIP;
    }


    /**
     * 获取账单信息
     */
    @ApiOperation("获取账单信息")
    @RequestMapping(value = "/get/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object getBill(@PathVariable("id") Integer id) {
        if(id == null) id=1;
        return this.service.getBill(id);
    }

}
